/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#pragma once
#include "Image.h"


#include <map>
#include <vector>

/**
========================
Create an image, and manage them
========================
*/
struct SDL_Surface;
class TextureLoader
{
public:
    TextureLoader(void);

    /** Test to see if the image exists */
    bool TextureAvailible( const unsigned int id) {
        return imageList.find( id ) != imageList.end();
    };

    /** Create a single image */
    Image CreateImage( const std::string &s );
    /** Create an image that contains subimages */
    Image CreateImage( const std::string &s, int width, int height );
    /** Free the image from memory */
    void FreeTexture( const unsigned int id);
    /** Delete all Textures */
    void FreeAll();
    /** number of textures in mem */
    size_t NumTextures() { return imageList.size(); };

private:
    /** Load the image from our lists */
    Image CheckInMemory( const std::string &s, int r, int c );
    /** Load Image */
    bool LoadImage(const std::string &file, int r, int c );
    /** Load the image into memory */
    bool LoadImageToMemory(SDL_Surface* surf, const std::string &file, int r, int c );

    /** Load a TGA */
    bool LoadTGA(const std::string &file, int r, int c);
    /** Load a BMP */
    bool LoadBMP(const std::string &file, int r, int c );

    /** Let OpenGL create a buffer for the image */
    void GenTexture( Image* image);
    /** Find the next power of two */
    int NextPowerOfTwo(int n);

    // image list by buffer id
    std::map< unsigned int, Image> imageList;
    // image list by file path
    std::map< std::string,  Image> loadedList;
    // image ids
    static unsigned int m_idcounter;

public:
    virtual ~TextureLoader(void);
};
